/**
 * 
 */
package de.uni_postdam.ling.tcl.smc_lda;

import java.util.Arrays;
import it.unimi.dsi.fastutil.ints.IntArrayList;

/**
 * 
 * @author christoph
 */
public class DocumentStorage
{
	/**
	 * 
	 */
	private long[] words = null;
	
	/**
	 * 
	 */
	private final IntArrayList topics = new IntArrayList();

	/**
	 * 
	 */
	private final long[] topicCounts;
	
	/**
	 * 
	 */
	private long sum;
	
	/**
	 * @param words
	 * @param topicCounts
	 */
	public DocumentStorage(int numberOfTopics)
	{
		super();
		this.topicCounts = new long[numberOfTopics];
	}

	/**
	 * 
	 * @return
	 */
	public long getSum()
	{
		return sum;
	}
	
	/**
	 * 
	 * @param topic
	 * @param amount
	 * @return
	 */
	public long addToTopicCount(int topic, long amount)
	{
		this.sum += amount;
		return this.topicCounts[topic] += amount;
	}
	
	/**
	 * 
	 * @param topic
	 * @return
	 */
	public long getTopicCount(int topic)
	{
		return this.topicCounts[topic];
	}
	
	/**
	 * 
	 * @param words
	 */
	public void reset(long[] words)
	{
		this.words = words;
		this.topics.clear();
		this.sum = 0;
		Arrays.fill(topicCounts, 0);
	}
	
	/**
	 * 
	 * @param pos
	 * @return
	 */
	public long getWord(int pos)
	{
		return this.words[pos];
	}
	
	/**
	 * 
	 * @param pos
	 * @return
	 */
	public int getTopic(int pos)
	{
		return this.topics.get(pos);
	}
	
	/**
	 * 
	 * @return
	 */
	public int getLength()
	{
		return this.words == null ? 0 : this.words.length;
	}
	
	/**
	 * 
	 * @param topic
	 */
	public void addTopic(int topic)
	{
		this.topics.add(topic);
	}
	
	/**
	 * @param pos
	 * @param topic
	 */
	public void setTopic(int pos, int topic)
	{
		this.topics.set(pos, topic);
	}

	/**
	 * 
	 * @param documentStorage
	 */
	public void copy(DocumentStorage documentStorage)
	{
		this.reset(documentStorage.words);
		for(int i=0;i<documentStorage.topics.size();++i)
		{
			this.addTopic(documentStorage.getTopic(i));
		}
		
		for(int i=0;i<documentStorage.topicCounts.length;++i)
		{
			this.addToTopicCount(i, documentStorage.topicCounts[i]);
		}
	}
}
